from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.urls import reverse
from .x23uscom import *
from .spider import *
from .dwl import *
# Create your views here.


def search_page(request):
    
    
    info_url = 'https://www.x23us.com/'
    x = Spider(url = info_url)
    info = x.info(book_info = '<dd><a href=".*?" target="_blank"><img src="(.*?)" alt=".*?"></a><br /><a href="https://www.x23us.com/html/(.*?)" target="_blank">(.*?)</a></dd>')
    info['book_info'] = info['book_info'][:8]
    #print(info)
    context = {'info':info}
    return render(request,'novel/search_page.html',context)
    

def search(request):
    if request.method == 'GET':
        return HttpResponseRedirect(reverse('novel:search_page',))
    elif request.method == 'POST':
        key_word = request.POST    
        key_word = key_word['key_word']
        res_obj = X23usCom(key_word)
        index_obj = res_obj.index()
        if isinstance(index_obj,(dict,)):
            url = index_obj['url']
            chapter = Spider(url)        
            info_url = chapter.info(chapter_url = '<a class="read" href="https://www.x23us.com/html/(.*?)" title=".*?">章节列表</a>')
            arg = info_url['chapter_url'][0].split('/')
            return HttpResponseRedirect(reverse('novel:novel_book',args=(arg[0],arg[1])))
        else:
            info = res_obj.info()
            context = {'info':info}
            return render(request,'novel/search.html',context)


def download(request,book_pk,book_id):
    
    url = 'https://www.x23us.com/html/{}/{}/'.format(book_pk,book_id)
        
    chapter_url = DownLoad(url)
    content = chapter_url.chapter()
    
    return HttpResponse(content)




def novel_book(request,book_pk,book_id):
    
    url = 'https://www.x23us.com/html/{}/{}/'.format(book_pk,book_id)

    book_name = '<meta name="og:novel:book_name" content="(.*?)"/>'
    book_author = '<meta name="og:novel:author" content="(.*?)"/>'
    book_info = '<td class="L"><a href="(.*?)">(.*?)</a></td>'

    x = Spider(url = url)
    info_list = x.info(book_name = book_name,book_author = book_author,book_info = book_info)
    #print(info_list['book_info'][0][0])
    return render(request,'novel/book.html',context=info_list)

    
def novel_chapter(request,book_pk,book_id,chapter_id):
    
    url = 'https://www.x23us.com/html/{}/{}/{}.html'.format(book_pk,book_id,chapter_id)

    chapter_title = '<title>(.*?)-(.*?)-(.*?)</title>'
    #print(chapter_title)
    chapter_content = '<dd id="contents">(.*)</dd>'
    #print(chapter_content)
    previou_page = '<dd id="footlink"><a href="/html/(.*?)">上一页</a><a href="/html/(.*?)" title=".*?">返回章节列表</a><a href="/html/(.*?)">下一页</a></dd>'

    x = Spider(url=url)
    info_list = x.info(chapter_title = chapter_title,chapter_content = chapter_content,previou_page = previou_page)
    a = info_list['chapter_content'][0].replace('顶点小说 Ｘ２３ＵＳ．ＣＯＭ更新最快','')
    info_list['chapter_content'][0] = a
    #print(info_list)
    return render(request,'novel/content.html',context = info_list)
    

    
